<?php

namespace CNEPAdmin\Controller;

use CNEPAdmin\Controller\CommonController;

class SysAdminRoleController extends CommonController {

    protected $_modelName = "SysAdminRole";

    protected function setCode() {
        $this->_code = "System";
    }

    public function index() {
        $this->display();
    }

    /**
     * 得到条件查询数组
     * @return Array
     */
    protected function _getQueryArr(&$where, &$order) {

        if (strlen(trim(I("sRoleName"))) > 0) {
            $where['roleName'] = array("like", "%" . trim(I("sRoleName") . "%"));
        }

//        $where['manType'] = 0;
        $where['isOn'] = 1;
        $where['isEnabled'] = I("isEnabled", 1);
        $_REQUEST['isEnabled'] = I("isEnabled", 1);

        $orderBy = 'addTime';
        $desc = 'desc';
        if (intval(I("sDesc")) === 1) {
            $desc = 'asc';
        }
        if (strlen(trim(I("sOrderBy", ''))) > 0) {
            $orderBy = I("sOrderBy");
        }

        $order = array(
            $orderBy => $desc
        );

        return $where;
    }

    /**
     * 查看列表
     */
    public function roleList() {
        $this->checkPriv('sysRole', 1, false);
        $this->assign('isEnabled', I("isEnabled", 1));
        $this->_getOnePageData("getRoleList", "getRoleListLength");
    }

    public function saveRole() {
        $id = I("roleID");
        
        if ($id > 0) {
            $this->_updateRoleMsg();
        } else {
            $this->_addRole();
        }
    }

    /**
     *  添加
     */
    private function _addRole() {
        $this->checkPriv('sysRole', 2);

        $this->_setModel();
        $rt = $this->_model->addRole();

        if ($rt) {
            $this->ajaxReturn(array('result' => 1));
        } else {
            $this->ajaxReturn(array('result' => 0, 'desc' => $this->_model->getLastSql. $this->_model->getError() . $this->_model->getDbError()));
        }
    }

    /**
     * 删除
     */
    public function deleteRole() {
        $this->checkPriv('sysRole', 2);

        $this->_setModel();
        $model = $this->_model;
        $rt = $model->deleteRole(I("post.roleID"));

        $rejson = $rt ? array('result' => 1) : array('result' => 0, 'desc' => $model->getError() . $model->getDbError());

        $this->ajaxReturn($rejson);
    }

    /**
     * 查看管理员信息
     */
    public function roleMsg() {
        $this->checkPriv('sysRole', 1, false);

        $roleid = intval(I("roleID", 0));

        if ($roleid > 0) { // 编辑
            $this->_setModel();
            $this->assign("arr", $this->_model->getRoleById($roleid));
        }

        $this->display();
    }

    /**
     * 更新信息
     */
    private function _updateRoleMsg() {
        $this->checkPriv('sysRole', 2);

        $this->_setModel();
        $admin = $this->_model;
        $rt = $admin->updateRole();
        $rejson = $rt ? array('result' => 1) : array('result' => 0, 'desc' => $admin->getError() . $admin->getDbError());

        $this->ajaxReturn($rejson);
    }

}
